<?php
class City extends AppModel {
	var $name = 'City';
	var $displayField = 'name';
	var $validate = array(
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $hasMany = array(		
		'CityDetail' => array(
			'className' => 'CityDetail',
			'foreignKey' => 'city_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => 'year',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Branch' => array(
			'className' => 'Branch',
			'foreignKey' => 'city_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'HumanResource' => array(
			'className' => 'HumanResource',
			'foreignKey' => 'city_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'DeviceResource' => array(
			'className' => 'DeviceResource',
			'foreignKey' => 'city_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'NhdHumanResource' => array(
			'className' => 'NhdHumanResource',
			'foreignKey' => 'city_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'NhdDeviceResource' => array(
			'className' => 'NhdDeviceResource',
			'foreignKey' => 'city_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'TreatmentRecord' => array(
			'className' => 'TreatmentRecord',
			'foreignKey' => 'city_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	
	var $hasAndBelongsToMany = array(
		'User' => array(
			'className' => 'User',
			'joinTable' => 'managers_cities',
			'foreignKey' => 'city_id',
			'associationForeignKey' => 'manager_id',
			'unique' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);
	
	public static function citiesByUser($user_id)
        {
		App::import('model','City');
		$city = new City();

		$cities = $city->find("all", array(
					'fields' => array('City.id','City.name'),
					'contain' => array(
						'User' => array(
							'conditions' => array('User.id' => $user_id)
						)
					)
				));
		foreach($cities as $key => $city)		
		{
			if(!count($city["User"]) || $city["User"][0]["id"] != $user_id)
			{
				unset($cities[$key]);
				//echo "-------------";
			}
		}
		return $cities;
        }

}
